package com.umeox.watch.moto.launcher.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.event.HorizontalScrollToPagerEvent;
import com.umeox.watch.moto.launcher.ui.HeartRateHistoryActivity;
import com.umeox.watch.moto.launcher.widget.AnalogClock;
import com.umeox.watch.moto.pedometer.util.StepManager;
import com.umeox.watch.moto.pedometer.util.StepUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class AnalogClockSimpleView extends RelativeLayout implements AnalogClock.OnTimeChangedListener, View.OnClickListener, StepManager.StepInfoChangeListener {

    private AnalogClock mAnalogClock;
    protected Context mContext;

    protected SimpleDateFormat mDateFormat = new SimpleDateFormat("EEE-dd", Locale.getDefault());
    private SimpleDateFormat mDateFormat_CN = new SimpleDateFormat("EEE-MM月dd日-a", Locale.getDefault());

    protected View mView;
    private View mHeartRateView;
    private View mPedometerView;
    private int mWallpaper;
    private MultiProgressBar mMultiProgressBar;

    public AnalogClockSimpleView(Context context) {
        this(context, null);
    }

    public AnalogClockSimpleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnalogClockSimpleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        //TODO
        mWallpaper = 0;//DataProvider.getWallpaper();
        initView();
        initWallpaper(mWallpaper);
        initData();
    }

    private void initData() {
        if (mMultiProgressBar == null) {
            return;
        }
        if (mWallpaper == 6) {
            mMultiProgressBar.setFirstColors(0xFFFFFFFF, 0xFFFFFFFF, 0xFF3C3C3C);
            mMultiProgressBar.setSecondColors(0xFFFFFFFF, 0xFFFFFFFF, 0xFF3C3C3C);
            mMultiProgressBar.setThirdColors(0xFFFFFFFF, 0xFFFFFFFF, 0xFF3C3C3C);
            return;
        }
        mMultiProgressBar.setFirstColors(0xFFFF1411, 0xFFFD08AB, 0xFF340914);
        mMultiProgressBar.setSecondColors(0xFF9BFF04, 0xFF9BFF04, 0xFF2C4204);
        mMultiProgressBar.setThirdColors(0xFF1AD5DE, 0xFF1AD5DE, 0xFF133D3C);


    }

    protected void initView() {
        mView = LayoutInflater.from(mContext).inflate(getLayoutId(), this);
        mAnalogClock = mView.findViewById(R.id.clock_analog);
        mPedometerView = mView.findViewById(R.id.iv_pedometer);
        mHeartRateView = mView.findViewById(R.id.iv_heart_rate);
        mMultiProgressBar = mView.findViewById(R.id.progress_multi);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mWallpaper == 6 || mWallpaper == 7)
            StepManager.getInstance().addStepInfoChangeListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        StepManager.getInstance().removeStepInfoChangeListener(this);
    }

    private int getLayoutId() {
        switch (mWallpaper) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 9:
                return R.layout.view_clock_analog_0;
            case 6:
                return R.layout.view_clock_analog_6;
            case 7:
                return R.layout.view_clock_analog_7;
            case 8:
                return R.layout.view_clock_analog_8;

        }

        return R.layout.view_clock_analog_0;
    }


    private void initWallpaper(int wallpaper) {
        final Resources resources = mContext.getResources();
        final String[] handHour = resources.getStringArray(R.array.hand_hour);
        final String[] handMinute = resources.getStringArray(R.array.hand_minute);
        final String[] handSecond = resources.getStringArray(R.array.hand_second);
        final int[] handNail = resources.getIntArray(R.array.hand_second_nail_offset);

        if (wallpaper == 7) {
            mAnalogClock.setSecondHandNailOffset(16);
        }

        if (wallpaper != 6) {
            mAnalogClock.setHandSecondsBitmap(getBitmap(handSecond[wallpaper]));
        }

        if (wallpaper == 9) {
            mPedometerView.setVisibility(VISIBLE);
            mPedometerView.setOnClickListener(this);
            mHeartRateView.setVisibility(VISIBLE);
        }

        if (mHeartRateView != null) {
            mHeartRateView.setOnClickListener(this);
        }


        mAnalogClock.setHandNail(handNail[wallpaper]);
        mAnalogClock.setHandHourBitmap(getBitmap(handHour[wallpaper]));
        mAnalogClock.setHandMinutesBitmap(getBitmap(handMinute[wallpaper]));
        mAnalogClock.setOnTimeChangedListener(this);
    }

    private Bitmap getBitmap(String name) {
        final int id = getResources().getIdentifier(name, "drawable", mContext.getPackageName());
//        return ResourceManager.getInstance().getBitmap(id);
        return BitmapFactory.decodeResource(mContext.getResources(),id);
    }

    @Override
    public void onTimeChange(long time) {
    }


    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.iv_pedometer:
                EventBus.getDefault().post(new HorizontalScrollToPagerEvent(0));
                break;
            case R.id.iv_heart_rate:
                Intent intent = new Intent(mContext, HeartRateHistoryActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                break;
        }
    }

    private static final String TAG = "AnalogClockSimpleView";

    @Override
    public void onStepInfoChange(int steps, int target, double height, double weight) {

        Logger.d(TAG, "onStepInfoChange:" + steps + " target:" + target + " height:" + height + " weight:" + weight);

        double stepDistance = StepUtils.getDistance(height);
        double distance = stepDistance * steps / 100;
        double calories = StepUtils.getKcal(weight, distance / 1000);

        mMultiProgressBar.setFirstProgress(steps * 100.0f / target);
        final double stepDistanceMax = stepDistance * 5000 / 100;
        final float distancePercent = (float) (distance * 100 / stepDistanceMax);
        mMultiProgressBar.setThirdProgress(distancePercent);
        mMultiProgressBar.setSecondProgress((float) (calories * 100 / StepUtils.getKcal(weight, stepDistanceMax / 1000)));
    }
}
